import 'dart:math';

import 'package:whale/helper/component/base/model/page.dart';
import 'package:whale/pages/refresh_demo/model.dart';

import '../../../../helper/component/base/refresh/tab_refresh/controller.dart';
import '../../../../helper/utils/util.dart';
import 'state.dart';

class TabDemoController extends ITabRefreshController<TabDemoState> {
  TabDemoController();

  void refreshTitle() async {
    await requestForTabTitles();
    onRefresh();
  }

  /// 在 widget 内存中分配后立即调用。
  @override
  void onInit() {
    super.onInit();
  }

  /// 在 onInit() 之后调用 1 帧。这是进入的理想场所
  @override
  void onReady() {
    super.onReady();
  }

  /// 在 [onDelete] 方法之前调用。
  @override
  void onClose() {
    super.onClose();
  }

  /// dispose 释放内存
  @override
  void dispose() {
    super.dispose();
  }

  @override
  TabDemoState createState() => TabDemoState();

  @override
  Future<NetBaseReponse> onRequest({int? current}) async {
    return NetBaseReponse.response(
        res: NetReponse.fromJson(
          {
            'code': 0,
            'data': {
              'current': 1,
              'records': [
                {'id': '1'}
              ]
            }
          },
        ),
        fromJsonT: (json) => PageModel.fromJson(
            json, (json) => RefreshDemoModel.fromJson(json)));
  }

  @override
  Future<void> requestForTabTitles() async {
    setupTabData(
        List.generate(Random().nextInt(8) + 2, (index) => '第$index个'), 0);
  }

  @override
  String idenPrefix() {
    return '222';
  }
}
